package com.wei.duowan.bbs.utils;

import android.opengl.GLES10;
import android.os.Build;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;

import com.wei.duowan.bbs.App;

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.egl.EGLSurface;

/**
 * 作者：赵若位
 * 时间：2019/9/27 14:35
 * 联系方式：1070138445
 * 功能：App应用内的相关配置
 */
public class Configuration
{
    private static final String TAG = Configuration.class.getSimpleName();

    /*系统包名*/
    private String mPackageName;
    /*屏幕宽度*/
    private int mScreenWidth;
    /*屏幕高度*/
    private int mScreenHeight;
    /*手机图片渲染高度*/
    private int mOpenglRender = -1;

    private static Configuration sConfiguration;

    private Configuration()
    {

    }

    public static Configuration getInstance()
    {
        if (sConfiguration == null)
        {
            synchronized (Configuration.class)
            {
                if (sConfiguration == null)
                {
                    sConfiguration = new Configuration();
                }
            }
        }
        return sConfiguration;
    }


    public int getScreenWidth()
    {
        if (mScreenWidth == 0)
        {
            DisplayMetrics metrics = App.getContext().getResources().getDisplayMetrics();
            return metrics.widthPixels;
        }
        return mScreenWidth;
    }

    public int getScreenHeight()
    {
        if (mScreenHeight == 0)
        {
            DisplayMetrics metrics = App.getContext().getResources().getDisplayMetrics();
            return metrics.heightPixels;
        }
        return mScreenHeight;
    }

    /*获取包名*/
    public String getPackName()
    {
        if (TextUtils.isEmpty(mPackageName))
        {
            mPackageName = App.getContext().getPackageName();
        }
        Log.e(TAG, "getPackName: "+mPackageName);
        return mPackageName;
    }


    public int getOpenglRender()
    {
        if (mOpenglRender == -1)
        {
            mOpenglRender = getOpenglRenderLimitValue();
        }
        return mOpenglRender;
    }


    /**
     * 获取手机显示最长图片的限制值
     *
     * @return
     */
    private int getOpenglRenderLimitValue()
    {
        int size;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        {
            size = getOpenglRenderLimitEqualAboveLollipop();
        } else
        {
            size = getOpenglRenderLimitBelowLollipop();
        }
        return size == 0 ? 4096 : size;
    }

    private int getOpenglRenderLimitBelowLollipop()
    {
        int[] maxSize = new int[1];
        GLES10.glGetIntegerv(GLES10.GL_MAX_TEXTURE_SIZE, maxSize, 0);
        return maxSize[0];
    }

    private int getOpenglRenderLimitEqualAboveLollipop()
    {
        EGL10 egl = (EGL10) EGLContext.getEGL();
        EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
        int[] vers = new int[2];
        egl.eglInitialize(dpy, vers);
        int[] configAttr = {
                EGL10.EGL_COLOR_BUFFER_TYPE, EGL10.EGL_RGB_BUFFER,
                EGL10.EGL_LEVEL, 0,
                EGL10.EGL_SURFACE_TYPE, EGL10.EGL_PBUFFER_BIT,
                EGL10.EGL_NONE
        };
        EGLConfig[] configs = new EGLConfig[1];
        int[] numConfig = new int[1];
        egl.eglChooseConfig(dpy, configAttr, configs, 1, numConfig);
        if (numConfig[0] == 0)
        {// TROUBLE! No config found.
        }
        EGLConfig config = configs[0];
        int[] surfAttr = {
                EGL10.EGL_WIDTH, 64,
                EGL10.EGL_HEIGHT, 64,
                EGL10.EGL_NONE
        };
        EGLSurface surf = egl.eglCreatePbufferSurface(dpy, config, surfAttr);
        final int EGL_CONTEXT_CLIENT_VERSION = 0x3098;// missing in EGL10
        int[] ctxAttrib = {
                EGL_CONTEXT_CLIENT_VERSION, 1,
                EGL10.EGL_NONE
        };
        EGLContext ctx = egl.eglCreateContext(dpy, config, EGL10.EGL_NO_CONTEXT, ctxAttrib);
        egl.eglMakeCurrent(dpy, surf, surf, ctx);
        int[] maxSize = new int[1];
        GLES10.glGetIntegerv(GLES10.GL_MAX_TEXTURE_SIZE, maxSize, 0);
        egl.eglMakeCurrent(dpy, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE,
                EGL10.EGL_NO_CONTEXT);
        egl.eglDestroySurface(dpy, surf);
        egl.eglDestroyContext(dpy, ctx);
        egl.eglTerminate(dpy);
        return maxSize[0];
    }


}
